package com.coder520.sync_learning.demo;

import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

public class SemaphoreTest {
	static volatile AtomicInteger sAtomicInteger=new AtomicInteger(0);
	private static final int THREAD_COUNT=30;

	private static ExecutorService threadService=Executors.newFixedThreadPool(THREAD_COUNT);
	
	private static Semaphore semaphore=new Semaphore(10);
	
	public static void main(String[] args) {
		for (int i = 0; i < THREAD_COUNT; i++) {
			threadService.execute(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					try {
						semaphore.acquire();
						System.out.println("save data:"+(sAtomicInteger.addAndGet(1)));
						semaphore.release();
					} catch (Exception e) {
						// TODO: handle exception
					}
				}
			});
		}
		threadService.shutdown();
	}
}
